/*
 * @Author: jhw
 * @Date: 2025-08-05 10:59:24
 * @LastEditors: jhw
 * @LastEditTime: 2025-08-15 15:01:25
 * @Description:
 */
import { defineStore } from 'pinia';
import { defaultStatusMap } from '@/configs/defaultStatus/defaultStatusMap';
import {
  removeOptionsStatus,
  setOptionsStatus,
  setPositionStatus,
  setTextStatus,
  setSizeStatus,
  setPicLinkByIndex,
  setTypeStatus,
  setNoteDescTypeStatus,
  setUse,
  setOptionsStatusByIndex,
  setSwitchStatus,
} from './actions';
import type { Status, SurveyComName } from '@/types';
import { updateInitStatusBeforeAdd } from '@/utils';

const keyToInit = ['personal-info-gender', 'personal-info-education'] as SurveyComName[];
const initializedStates: { [key: string]: Status } = {};
keyToInit.forEach((key) => {
  const defaultStatus = defaultStatusMap[key]() as Status;
  updateInitStatusBeforeAdd(defaultStatus, key);
  initializedStates[key] = defaultStatus;
});

export const useMaterialStore = defineStore('materialStore', {
  state: () => ({
    // 所有组件列表
    coms: {
      'single-select': defaultStatusMap['single-select'](),
      'single-pic-select': defaultStatusMap['single-pic-select'](),
      'text-input': defaultStatusMap['text-input'](),
      'multiple-select': defaultStatusMap['multiple-select'](),
      'option-select': defaultStatusMap['option-select'](),
      'note-desc': defaultStatusMap['note-desc'](),
      'form-group': defaultStatusMap['form-group'](),
      'personal-info-gender': initializedStates['personal-info-gender'],
      'personal-info-education': initializedStates['personal-info-education'],
      'personal-info-score': defaultStatusMap['personal-info-score'](),
      'date-time': defaultStatusMap['date-time'](),
    },
    // 当前选中的组件
    currentMaterialCom: 'single-select',
  }),
  actions: {
    setTextStatus,
    setOptionsStatus,
    removeOptionsStatus,
    setPositionStatus,
    setSizeStatus,
    setPicLinkByIndex,
    setTypeStatus,
    setNoteDescTypeStatus,
    setUse,
    setOptionsStatusByIndex,
    setSwitchStatus,
    // 设置当前选中的组件
    setCurrentMaterialCom(comName: string) {
      this.currentMaterialCom = comName;
    },
  },
});
